# coding=utf-8
import sys
import os
import ddt
import unittest
import HTMLTestRunner
from Base.base_request import request
from Util.handle_header import get_header
from Util.handle_result import handle_result, get_result_json, handle_result_json
from Util.handle_token import get_token, write_token, get_token_json
from Util.handle_excel import excel_data
import json
from Util.codition_data import get_data

# from collections.abc import Iterable
base_path = os.getcwd()
base_path_config = os.path.abspath(os.path.join(os.path.dirname('settings.py'), os.path.pardir))
sys.path.append(base_path)

data = excel_data.get_excel_data(1)


@ddt.ddt
class TestRunCaseDdt(unittest.TestCase):

    @ddt.data(*data)
    def test_main_case(self,data):
        header = None
        depend_data = None

        is_run = data[2]
        case_id = data[0]
        i = excel_data.get_rows_number(case_id,1)
        if is_run == 'yes':
            is_depend = data[3]
            json_params = json.loads(data[7])
            try:
                if is_depend:
                    '''
                    获取依赖数据
                    依赖模块
                    '''
                    depend_key = data[4]
                    depend_data = get_data(is_depend)
                    # print(depend_data)
                    json_params[depend_key] = depend_data

                method = data[6]
                url = data[5]

                is_header = data[9]
                excepect_method = data[10]
                excepect_result = data[11]
                # print(type(excepect_result))
                token_method = data[8]
                if is_header == "yes":

                    if token_method == 'yes':
                        res_token = get_token()
                        write_token("Bearer " + res_token, "Authorization")
                        header = get_token_json()

                    elif token_method == None:
                        header = get_token_json()
                    else:
                        header = get_header()

                res = request.run_main(method, url, json_params, header)
                # print(res.get("data"))

                res_code = str(res.get('code'))
                message = res.get('message')
                if excepect_method == 'mec':
                    config_message = handle_result(url, res_code)
                    try:
                        self.assertEqual(message, config_message)
                        excel_data.excel_write_data(i, 13, "通过")
                        excel_data.excel_write_data(i, 14, json.dumps(res, ensure_ascii=False))
                    except Exception as e:
                        excel_data.excel_write_data(i, 13, "失败")
                        raise e

                if excepect_method == 'ExcepectCode':
                    try:
                        self.assertEqual(excepect_result, res_code)
                        excel_data.excel_write_data(i, 13, "通过")
                    except Exception as e:
                        excel_data.excel_write_data(i, 13, "失败")
                        raise e

                if excepect_method == 'json':
                    if res_code == '0':
                        status_str = 'data'
                    else:
                        status_str = 'error'
                    excepect_result = get_result_json(url, status_str)
                    result = handle_result_json(res, excepect_result)
                    try:
                        self.assertTrue(result)
                        excel_data.excel_write_data(i, 13, "通过")
                    except Exception as e:
                        excel_data.excel_write_data(i, 13, "失败")
                        raise e
            except Exception as e:
                excel_data.excel_write_data(i, 13, "失败")
                raise e


if __name__ == "__main__":
    # case_path = base_path + "/Run"
    # case_path = base_path
    # report_path = base_path + "/Report/report.html"
    report_path = base_path_config + "/Report/report.html"
    discover = unittest.defaultTestLoader.discover(base_path, pattern="run_case_*.py")
    # unittest.TextTestRunner().run(discover)
    with open(report_path, "wb") as f:
        runner = HTMLTestRunner.HTMLTestRunner(stream=f, title="Mushishi", description="this is test")
        runner.run(discover)
